<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class CoreValidate {
	//data
	var $_result_data = array();
	//flag
	var $_result_valid = true;
	
	//constructor
	function CoreValidate() {
		
	}
	
	//methods
	function _add($key, $value) {
		$this->_result_data[$key] = $value;
	}
	
	function _append($key, $value) {
		if( isset($this->_result_data[$key]) ) {
			$this->_result_data[$key][] = $value;
		} else {
			$this->_result_data[$key] = $value;
		}
	}
	
	function _get($key = '') {
		if( $key == '' ) {
			return $this->_result_data;
		} elseif( isset($this->_result_data[$key]) ) {
			return $this->_result_data[$key];
		}
	}
	
	function _remove($key) {
		if( isset($this->_result_data[$key]) ) {
			unset($this->_result_data[$key]);
		}
	}
	
	function _clean() {
		$this->_result_data = array();
	}
	
	function _reset() {
		$this->_result_data = array();
	}
	
	//
	function _valid() {
		if( sizeof($this->_result_data) > 0 || $this->_result_valid == false ) {
			return false;
		} else {
			return true;
		}
	}
	
	function _outputJs() {
		$output_str = '';
		
		if( sizeof($this->_result_data) > 0 ) {
			$output_str .= '';
			for($i=0, $n=sizeof($this->_result_data); $i<$n; $i++) {
				$output_str .= $this->_result_data[$i] . "\n";
			}
		}
		
		return $output_str;
	}
	
	function _output() {
		$output_str = '';
		
		if( sizeof($this->_result_data) > 0 ) {
			$output_str .= '';
			foreach($this->_result_data as $k => $v) {
				$output_str .= $v . "\n";
			}
		}
		
		return $output_str;
	}
	
	function _result($title = '', $msg = '', $show = true) {
		if( $title == '' ) {
			$title = ':error';
		}
		if( substr($title, 0, 1) == ':' ) {
			$title = _lang(':error' . $title, 'error');;
		}
		if( $msg == '' ) {
			$msg = $this->_output();
		} elseif( substr($msg, 0, 1) == ':' ) {
			global $LANGUAGE;
			
			$msg = substr($msg, 1);
			$msg = isset($LANGUAGE['error'][$msg]) ? $LANGUAGE['error'][$msg] : $msg;
		}
		return array('title' => $title, 'message' => $msg, 'show' => $show);
	}
	
	function _valid_result() {
		return $this->_result_data;
	}
	
	//
	function _not_null($value) {
		return util_not_null($value);
	}
	
	function _is_null($value) {
		if( is_array($value) ) {
			if( sizeof($value) > 0 ) {
				return false;
			} else {
				return true;
			}
		} else {
			if( (is_string($value) || is_int($value) || is_numeric($value)) && ($value != '') && ($value != 'NULL') && (strlen(trim($value)) > 0) ) {
				return false;
			} else {
				return true;
			}
		}
	}
	
	function _is_num($value) {
		if( is_numeric($value) ) {
			return true;
		}
		
		return false;
	}
	
	function _num($value) {
		
	}
	
	function _lt($value, $dest) {
		
	}
	
	function _le($value, $dest) {
		
	}
	
	function _eq($value, $dest) {
		
	}
	
	function _gt($value, $dest) {
		
	}
	
	function _ge($value, $dest) {
		
	}
	
	function _str_null($string) {
		if( is_array($string) ) {
			if( sizeof($string) > 0 ) {
				return false;
			} else {
				return true;
			}
		} else {
			if( (is_string($string) || is_int($string) || is_numeric($string)) && ($string != '') && ($string != 'NULL') && (strlen(trim($string)) > 0) ) {
				return false;
			} else {
				return true;
			}
		}
	}
	
	function _str_lt($string, $count = 5) {
		if( strlen($string) < $count ) {
			return true;
		}
		
		return false;
	}
	
	function _str_le($string, $count = 5) {
		if( strlen($string) < $count ) {
			return true;
		}
		
		return false;
	}
	
	function _str_eq($string, $count = 5) {
		if( strlen($string) == $count ) {
			return true;
		}
		
		return false;
	}
	
	function _str_gt($string, $count = 5) {
		if( strlen($string) > $count ) {
			return true;
		}
		
		return false;
	}
	
	function _str_ge($string, $count = 5) {
		if( strlen($string) > $count ) {
			return true;
		}
		
		return false;
	}
	
	function _num_lt($num, $value = 5) {
		if( strlen($num) < $value ) {
			return true;
		}
		
		return false;
	}
	
	function _num_le($num, $value = 5) {
		if( strlen($num) < $value ) {
			return true;
		}
		
		return false;
	}
	
	function _num_eq($num, $value = 5) {
		if( strlen($num) == $value ) {
			return true;
		}
		
		return false;
	}
	
	function _num_gt($num, $value = 5) {
		if( strlen($num) > $value ) {
			return true;
		}
		
		return false;
	}
	
	function _num_ge($num, $value = 5) {
		if( strlen($num) > $value ) {
			return true;
		}
		
		return false;
	}
	
	function _num_btw($num, $start, $end) {
		if( $num > $start && $num < $end ) {
			return true;
		}
		
		return false;
	}
	
	function _num_btw_ee($num, $start, $end) {
		if( $num >= $start && $num <= $end ) {
			return true;
		}
		
		return false;
	}
	
	function _num_btw_xe($num, $start, $end) {
		if( $num > $start && $num <= $end ) {
			return true;
		}
		
		return false;
	}
	
	function _num_btw_ex($num, $start, $end) {
		if( $num >= $start && $num < $end ) {
			return true;
		}
		
		return false;
	}
	
	function _valid_email($email) {
		return util_validate_email($email);
	}
	
	//
	function _valid_type($value, $pattern) {
		$patterns = array(
				'require'=> '/.+/',
				'email' => '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',
				'url' => '/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/',
				'currency' => '/^\d+(\.\d+)?$/',
				'zip' => '/^[1-9]\d{5}$/',
				'number' => '/\d+$/',
				'integer' => '/^[-\+]?\d+$/',
				'float' => '/^[-\+]?\d+(\.\d+)?$/',
				'double' => '/^[-\+]?\d+(\.\d+)?$/',
				'english' => '/^[A-Za-z]+$/',
					);
		if( isset($patterns[$pattern]) ) {
			$pattern = $patterns[$pattern];
		}
		
		if( preg_match($pattern, $value) === 1 ) {
			return true;
		}
		
		return false;
	}
	
	//
	function check($data_array, $fields) {
		return true;
	}
	
	function checkNull($data_array, $fields) {
		if( !is_array($data_array) ) {
			return true;
		}
		foreach($data_array as $k => $v) {
			if( substr($k, 0, strlen('field_')) == 'field_' ) {
				$kb = substr($k, strlen('field_'));
			}
			if( (in_array($k, $fields) || in_array($kb, $fields)) && $this->_str_eq($data_array[$k], 0) == true ) {
				$this->_result_valid = false;
			}
		}
	}
	
	function checkEmail($email) {
		if( $this->_valid_email($email) == false ) {
			$this->_result_valid = false;
		}
	}
	
	function checkColor($rgb) {
		$rgb = strtolower($rgb);
		if( strlen($rgb) == 8 && substr($rgb, 0, 2) == '0x' ) {
			$rgb = substr($rgb, 2);
		}
		if( strlen($rgb) == 6 && preg_match("[0-9a-f]", $rgb) ) {
			//$this->_result_valid = true;
		} else {
			$this->_result_valid = false;
		}
	}
}
//
?>